﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace Billing.WindowsBase.CustomControls
{
    public delegate void BootonClickHandler(object sender, Infragistics.Win.UltraWinEditors.EditorButtonEventArgs e);
    public delegate void InitializedHandler(object sender);
    //  public delegate void ValueChangedHandler(object value);
    public delegate void NewValueHandler(IBaseObject newValue);

    [ToolboxItem(true)]
    [DesignerAttribute(typeof(sbThreeValueEditorDesigner), typeof(System.ComponentModel.Design.IDesigner))]
    [DefaultEvent("Initialized")]
    [ToolboxBitmap(typeof(System.Windows.Forms.TextBox))]
    public partial class sbThreeValueEditor : BaseControl
    {
        public sbThreeValueEditor()
        {
            InitializeComponent();
        }


        public event BootonClickHandler GoBootonClick;
        public event BootonClickHandler DeleteBootonClick;
        public event BootonClickHandler ChooseBootonClick;
        public event BootonClickHandler InfoBootonClick;
        public event InitializedHandler Initialized;
        //public event ValueChangedHandler ValueChanged;
        public event NewValueHandler NewValue;

        public event KeyEventHandler TextBoxKeyUp;

        #region Private Fildes
        private IBaseObject oldValue;
        private string oldText;
        #endregion

        #region Public Properties

        private IBaseObject baseObject;
        public IBaseObject BaseObject
        {
            get { return baseObject; }
            set
            {
                baseObject = value;
                if (baseObject == null)
                {
                    this.cmb.Items.Clear();
                    this.cmb.Value = null;
                    this.lblDescription.Text = string.Empty;
                }
                else
                {
                    this.cmb.Items.Clear();
                    this.cmb.Items.Add(baseObject.Value, baseObject.Key);

                    this.cmb.Value = baseObject.Value;
                    this.lblDescription.Text = value.Name;
                    Infragistics.Win.UltraWinToolTip.UltraToolTipInfo ttinf = new Infragistics.Win.UltraWinToolTip.UltraToolTipInfo();
                    ttinf.ToolTipText = this.lblDescription.Text;
                    ttinf.ToolTipImage = Infragistics.Win.ToolTipImage.Info;
                    this.ultraToolTipManager1.SetUltraToolTip(this.lblDescription, ttinf);
                }

                if (this.NewValue != null)
                    this.NewValue(baseObject);
            }
        }
        [Category("Custom")]
        public bool AllowClear
        {
            get { return this.getButtonVisible("clear"); }
            set { this.setButtonVisible("clear", value); }
        }
        [Category("Custom")]
        public bool AllowGo
        {
            get { return this.getButtonVisible("go"); }
            set { this.setButtonVisible("go", value); }
        }
        [Category("Custom")]
        public bool AllowCoose
        {
            get { return this.getButtonVisible("choose"); }
            set { this.setButtonVisible("choose", value); }
        }
        [Category("Custom")]
        public bool AllowInfo
        {
            get { return this.getButtonVisible("Info"); }
            set { this.setButtonVisible("Info", value); }
        }

        [Category("Custom")]
        public Size NameSize
        {
            get { return this.lblName.Size; }
            set { this.lblName.Size = value; }
        }
        [Category("Custom")]
        public string NameText
        {
            get { return this.lblName.Text; }
            set { this.lblName.Text = value; }
        }

        [Bindable(true), Category("Custom")]
        public string DescriptionText
        {
            get { return this.lblDescription.Text; }
            set { this.lblDescription.Text = value; }
        }

        [Bindable(true), Browsable(true)]
        public object Value
        {
            get { return this.cmb.Value; }
            set { this.cmb.Value = value; }
        }
        [Bindable(true), Browsable(true)]
        public new string Text
        {
            get { return this.cmb.Text; }
            set { this.cmb.Text = value; }
        }
        [Category("Custom")]
        public bool ReadOnly
        {
            get { return this.cmb.ReadOnly; }
            set { this.cmb.ReadOnly = value; }
        }
        #endregion

        private void cmb_EditorButtonClick(object sender, Infragistics.Win.UltraWinEditors.EditorButtonEventArgs e)
        {
            switch (e.Button.Key.ToLower())
            {
                case "go":
                    if (this.GoBootonClick != null)
                        this.GoBootonClick(this, e);
                    break;
                case "clear":
                    if (this.DeleteBootonClick != null)
                        this.DeleteBootonClick(this, e);
                    this.cmb.Items.Clear();
                    this.cmb.Value = DBNull.Value;
                    this.cmb.Text = String.Empty;
                    this.lblDescription.Text = string.Empty;
                    break;
                case "choose":
                    if (this.ChooseBootonClick != null)
                    {
                        this.ChooseBootonClick(this, e);
                    }
                    break;
                case "info":
                    if (this.InfoBootonClick != null)
                        this.InfoBootonClick(this, e);
                    break;
                default:
                    break;
            }
        }

        private void cmb_BeforeDropDown(object sender, CancelEventArgs e)
        {
            e.Cancel = true;
        }

        public void Initialize()
        {
            if (this.Initialized != null)
                this.Initialized(this);
            this.oldText = this.cmb.Text;
        }

        private void setButtonVisible(string key, bool value)
        {
            this.cmb.ButtonsRight[key].Visible = value;
        }

        private bool getButtonVisible(string key)
        {
            return this.cmb.ButtonsRight[key].Visible;
        }

        private void cmb_KeyUp(object sender, KeyEventArgs e)
        {
            if (this.TextBoxKeyUp != null)
                this.TextBoxKeyUp(sender, e);
        }



        #region Label
        [Category("Custom")]
        public override string LabelText
        {
            get { return this.lblName.Text; }
            set
            {
                this.lblName.Text = value;
                if (this.LabelAutoSize)
                {
                    AutoSizeLabel();
                }
            }
        }

        private void AutoSizeLabel()
        {
            System.Drawing.Graphics gr = this.CreateGraphics();
            this.lblName.Size = new Size((int)gr.MeasureString(this.lblName.Text, this.lblName.Font).Width + 8, this.lblName.Height);
        }
        [Category("Custom")]
        public override bool LabelAutoSize
        {
            get
            {
                return base.LabelAutoSize;
            }
            set
            {
                base.LabelAutoSize = value;
                if (this.LabelAutoSize)
                {
                    AutoSizeLabel();
                }
            }
        }
        [Category("Custom")]
        public override Size LabelSize
        {
            get { return this.lblName.Size; }
            set { this.lblName.Size = value; }
        }
        [Category("Custom")]
        public override bool LabelVisible
        {
            get { return this.lblName.Visible; }
            set { this.lblName.Visible = value; }
        }

        public override Color LabelBorderColor
        {
            get { return this.lblName.Appearance.BorderColor; }
            set { this.lblName.Appearance.BorderColor = value; }
        }
        #endregion

        #region Editor
        [Category("Custom")]
        public override string EditorText
        {
            get { return this.cmb.Text; }
            set { this.cmb.Text = value; }
        }
        [Category("Custom")]
        public override Size EditorSize
        {
            get { return this.cmb.Size; }
            set { this.cmb.Size = value; }
        }
        [Category("Custom")]
        public override bool EditorVisible
        {
            get { return this.cmb.Visible; }
            set { this.cmb.Visible = value; }
        }
        [Category("Custom")]
        public override object EditorSource
        {
            get { return this.cmb.DataSource; }
            set { this.cmb.DataSource = value; }
        }
        [Category("Custom")]
        public override object EditorValue
        {
            get { return this.cmb.Value; }
            set { this.cmb.Value = value; }
        }
        [Category("Custom")]
        public override bool EditorIsReadOnly
        {
            get { return this.cmb.ReadOnly; }
            set { this.cmb.ReadOnly = value; }
        }
        #endregion
    }

    #region "Designer
    public class sbThreeValueEditorDesigner : System.Windows.Forms.Design.ControlDesigner
    {
        public override System.Windows.Forms.Design.SelectionRules SelectionRules
        {
            get
            {
                System.Windows.Forms.Design.SelectionRules i = System.Windows.Forms.Design.SelectionRules.RightSizeable |
                    System.Windows.Forms.Design.SelectionRules.Visible |
                    System.Windows.Forms.Design.SelectionRules.LeftSizeable |
                    System.Windows.Forms.Design.SelectionRules.Moveable;

                return i;
            }
        }
    }
    #endregion
}
